import i18n from '@/i18n'
import { FlowTemplateDao } from '@apps/flow/domain/flow-template/flow-template.dao'
import { DomainModel } from '@apps/flow/domain/model'
import { Assist } from '@libs/lhy-common'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { FlowTemplateBatchSaveDto } from './dto/flow-template-batch-save.dto'
import { FlowTemplateListGroupDto } from './dto/flow-template-list-group.dto'
import { FlowTemplatePageDto } from './dto/flow-template-page.dto'
import { FlowTemplateSaveDto } from './dto/flow-template-save.dto'
import { FlowTemplateTreeDto } from './dto/flow-template-tree.dto'
import { FlowTemplateUpdateDto } from './dto/flow-template-update.dto'
import { FlowTemplateService } from './flow-template.service'
import { ApiResponsePage } from '@libs/lhy-core'

@ApiTags(i18n.t('app-flow.flow-template.flow-template.12152228-0'))
@Controller({ path: 'template', version: '1' })
export class FlowTemplateController {
  constructor(
    private readonly templateDao: FlowTemplateDao,
    private readonly templateService: FlowTemplateService
  ) {}

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-1') })
  @Get('page')
  @ApiResponsePage(DomainModel.FlowTemplate)
  async page(@Query() param: FlowTemplatePageDto) {
    return this.templateDao.page(param)
  }

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-2') })
  @Get('list_group')
  async listGroup(@Query() param: FlowTemplateListGroupDto) {
    return this.templateDao.listGroup(param)
  }

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-3') })
  @Get('tree')
  async tree(@Query() param: FlowTemplateTreeDto) {
    return this.templateService.tree(param)
  }

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-4') })
  @Put('save')
  async save(@Body() param: FlowTemplateSaveDto) {
    Assist.threw(
      await this.templateDao.isLabelExists([
        {
          label: param.label,
          groupLabel: param.groupLabel,
          platform: param.platform,
        },
      ]),
      i18n.t('app-flow.flow-template.flow-template.12152228-5', { args: [param.label] })
    )

    await this.templateDao.save({
      ...param,
      belong: DomainModel.FlowTemplateBelong.Platform,
    })
  }

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-6') })
  @Put('batch_save')
  async batchSave(@Body() param: FlowTemplateBatchSaveDto) {
    if (param.cover) {
      await this.templateDao.batchUpsertOnUnique(param.templates, DomainModel.FlowTemplateBelong.Platform)
    } else {
      Assist.threw(
        await this.templateDao.isLabelExists(
          param.templates.map(template => ({
            platform: template.platform,
            groupLabel: template.groupLabel,
            label: template.label,
          }))
        ),
        i18n.t('app-flow.flow-template.flow-template.12152228-7')
      )

      await this.templateDao.batchSave(
        param.templates.map(template => ({
          ...template,
          belong: DomainModel.FlowTemplateBelong.Platform,
        }))
      )
    }
  }

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-8') })
  @Post('update')
  async update(@Body() param: FlowTemplateUpdateDto) {
    const oldData = Assist.notNull(
      await this.templateDao.getById(param.id),
      i18n.t('app-flow.flow-template.flow-template.12152228-9', { args: [param.id] })
    )
    Assist.threw(
      oldData.belong === DomainModel.FlowTemplateBelong.System,
      i18n.t('app-flow.flow-template.flow-template.12152228-10')
    )
    Assist.threw(
      await this.templateDao.isLabelExists([{ ...param, platform: oldData.platform }]),
      i18n.t('app-flow.flow-template.flow-template.12152228-5', { args: [param.label] })
    )

    await this.templateDao.updateById(param.id, param)
  }

  @ApiOperation({ summary: i18n.t('app-flow.flow-template.flow-template.12152228-11') })
  @Delete('del')
  async del(@Query('id') id: number) {
    const oldData = Assist.notNull(
      await this.templateDao.getById(id),
      i18n.t('app-flow.flow-template.flow-template.12152228-12', { args: [id] })
    )
    Assist.threw(
      oldData.belong === DomainModel.FlowTemplateBelong.System,
      i18n.t('app-flow.flow-template.flow-template.12152228-13')
    )

    await this.templateDao.deleteById(id)
  }
}
